package com.dh.example.netty.listener;

import com.dh.example.netty.entity.User;
import com.dh.example.netty.entity.event.UserOfflineEvent;
import com.dh.example.netty.service.WebSocketService;
import com.dh.example.netty.util.WsMsgBuilderAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 用户脱机侦听器
 *
 * @author yolo
 * @date 2024/03/15 11:11:02
 */
@Slf4j
@Component
public class UserOfflineListener {
    @Resource
    private WebSocketService webSocketService;


    @Async
    @EventListener(classes = UserOfflineEvent.class)
    public void saveRedisAndPush(UserOfflineEvent event) {
        User user = event.getUser();
        //推送给所有在线用户，该用户下线
        webSocketService.sendToAllOnline(WsMsgBuilderAdapter.buildOfflineNotifyResp(user));
    }

}
